// pages/management/management.js

const cloud = require('../../../../utils/cloudFunction')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        clock: '',
        roomList: [],
        isAddRoom: false,
        isEditRoom: false,
        placeholderName: 'name',
        tmpId: 0,
        entryFlag: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // var that = this
        this.setData({
            roomList: app.globalData.homeList
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this
        // getHomeList
        this.data.clock = setInterval(() => {
            // console.log("room management clocking ... ")
            if (app.globalData.refreshStatus) {
                that.setData({
                    roomList: app.globalData.homeList
                })
                app.globalData.refreshStatus = false
                console.log("roomManagement update -------------------------")
            }
        }, 500);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.clock)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    editRoom(e) {
        this.setData({
            isEditRoom: true,
            placeholderName: e.target.dataset.roomname,
            tmpId: e.target.dataset.roomid,
        })
    },

    deleteRoom(e) {
        cloud.roomDelete(e.target.dataset.roomid).then(function name(params) {
            cloud.changeStatus()
            wx.showToast({
                title: '删除房间成功',
                duration: 2000
            })
        })
        // this.refreshRoom()
    },

    //roomCard的取消按钮
    onEditCancel() {
        this.setData({
            isEditRoom: false,
        })
    },
    //roomCard的确定按钮
    onEditConfirm(e) {
        cloud.saveOrUpdateRoomInfo(e.detail, this.data.tmpId).then(function name(params) {
            cloud.changeStatus()
        })

        wx.showToast({
            title: '修改房间名称成功',
            duration: 2000
        })
        // this.refreshRoom()
        this.setData({
            isEditRoom: false,
        })
    },

    //+号的取消按钮
    onAddCancel() {
        this.setData({
            isAddRoom: false,
        })
    },
    //+号的确定按钮
    onAddConfirm(e) {
        cloud.saveOrUpdateRoomInfo(e.detail).then(function name(params) {
            cloud.changeStatus()
            wx.showToast({
                title: '添加房间成功',
                duration: 2000
            })
        })
        this.setData({
            isAddRoom: false,
        })
        // this.refreshRoom()
    },

    addRoom() {
        this.setData({
            isAddRoom: true
        })
    },

})